package elitech.vietnam.myfashion.view;

import java.util.Locale;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.entites.OrderDetail;
import elitech.vietnam.myfashion.utilities.Utilities;

public class OrderDetailView extends LinearLayout {
	private static final int SPACE = 40;
	private TextView mTxtName, mTxtPrice, mTxtColor, mTxtSize, mTxtQuantity,
			mTxtAmount;
	public LinearLayout mLayDelete;
	private ImageView mImage;
	public ImageView mBtnDelete;

	public OrderDetailView(MainActivity context, OrderDetail item) {
		super(context);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (inflater != null) {
			inflater.inflate(R.layout.new_item_bill, this);
		}

		mImage = (ImageView) findViewById(R.id.orderdetail_image);
		mBtnDelete = (ImageView) findViewById(R.id.orderdetail_btnDelete);
		mLayDelete = (LinearLayout) findViewById(R.id.orderdetail_layDelete);
		mTxtName = (TextView) findViewById(R.id.orderdetail_txtName);
		mTxtPrice = (TextView) findViewById(R.id.orderdetail_txtPrice);
		mTxtColor = (TextView) findViewById(R.id.orderdetail_txtColor);
		mTxtSize = (TextView) findViewById(R.id.orderdetail_txtSize);
		mTxtQuantity = (TextView) findViewById(R.id.orderdetail_txtQuantity);
		mTxtAmount = (TextView) findViewById(R.id.orderdetail_txtAmount);

		mTxtName.setText(item.ProductName);
		if (item.Status == 0)
			mTxtName.setPaintFlags(mTxtName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
		String price = context.getString(R.string.pricewithoutcolon);
		mTxtPrice.setText(price + MakeSpace(SPACE - price.length())
				+ Utilities.numberFormat(item.PriceVN) + " VND");

		String color;
		if (context.getLocale().equals(Locale.ENGLISH)) {
			if (item.ColorNameEN != null) {
				color = context.getString(R.string.color);
				mTxtColor.setText(color + MakeSpace(SPACE-color.length()) + item.ColorNameEN);
			} else
				mTxtColor.setVisibility(View.GONE);
		} else if (context.getLocale().equals(Locale.KOREA)) {
			if (item.ColorNameKR != null) {
				color = context.getString(R.string.color);
				mTxtColor.setText(color + MakeSpace(SPACE-color.length()) + item.ColorNameKR);
			} else
				mTxtColor.setVisibility(View.GONE);
		} else {
			if (item.ColorNameVN != null) {
				color = context.getString(R.string.color);
				mTxtColor.setText(color + MakeSpace(SPACE-color.length()) + item.ColorNameVN);
			} else
				mTxtColor.setVisibility(View.GONE);
		}

		if (item.SizeName != null) {
			String size = context.getString(R.string.size);
			mTxtSize.setText(size + MakeSpace(SPACE-size.length()) + item.SizeName);
		} else
			mTxtSize.setVisibility(View.GONE);

		String quantity = context.getString(R.string.quantity);
		mTxtQuantity.setText(quantity + MakeSpace(SPACE-quantity.length()-3) + item.Quantity + "");
		
		String amount = context.getString(R.string.amount);
		mTxtAmount.setText(amount + MakeSpace(SPACE-amount.length()-4) + Utilities.numberFormat(item.getAmountVN())
				+ " VND");
		
		UrlImageViewHelper.setUrlDrawable(mImage, Const.SERVER_IMAGE_THUMB_URL
				+ item.Image, R.drawable.loading150);
		mBtnDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				event.onCancel();
			}
		});

	}

	public String MakeSpace(int length) {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < length; i++)
			builder.append(" ");
		return builder.toString();
	}

	public OrderDetailViewEvent event;

	public void setOnCancelEvent(OrderDetailViewEvent mEvent) {
		this.event = mEvent;
	}

	public interface OrderDetailViewEvent {
		public void onCancel();
	};

}
